﻿using System;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AddEditCertification : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditCertification_Update;
            this.Delete += AddEditCertification_Delete;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Components.Certification certification = Components.Certification.Load(this.ItemID);
                    if (certification != null)
                    {
                        txtTitle.Text = certification.Title;
                        txtDescription.Text = certification.Description;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        void AddEditCertification_Update(object sender, EventArgs e)
        {
            Components.Certification certification = new Components.Certification();
            if (this.ItemID > 0)
            {
                certification = Components.Certification.Load(this.ItemID);
            }

            certification.ModuleID = this.ModuleID;
            certification.Title = txtTitle.Text;
            certification.Description = txtDescription.Text;

            // Insert.
            if (this.ItemID == 0)
            {
                this.ItemID = certification.Insert();
            }
            // Update.
            else
            {
                certification.CertificationID = this.ItemID;
                certification.Update();
            }

            // Go back.
            this.ReturnToReferrerPage();

        }

        void AddEditCertification_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Certification.DeleteCertification(this.ItemID);
                // Go back.
                this.ReturnToReferrerPage();
            }
        }
    }
}
